<?php
namespace app\content\controller;

use app\model\Classes;
use app\model\ClassPlan as ModelClassPlan;
use app\model\ClassPlanTask as ModelClassPlanTask;
use app\model\ClassTask as ModelClassTask;
use app\model\ClassUser;
use app\model\Company;
use app\model\Exampaper;
use app\model\Product;
use app\model\ProductChapter as ModelProductChapter;
use app\model\ProductCourse as ModelProductCourse;
use app\model\User;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

/**
 * 打卡任务
 * @class ClassTask
 * @package app\content\controller
 */
class ClassTask extends Controller
{
  
    /**
     * 打卡任务
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        ModelClassTask::mQuery()->layTable(function () {
            $this->title = '课程视频';
            $this->class_list = Classes::mk()->where(['status' => 1])->field('id,name')->select();
            $this->teacher_list = Company::mk()->field('id,name')->select();
        }, function (QueryHelper $query) {

            // 加载对应数据列表
            // $query->where(['is_deleted' => 0, 'status' => intval($this->type === 'index')]);

            // 关联用户身份资料
            /** @var \think\model\Relation|\think\db\Query $query */
            $query->with(['classes' => static function ($query) {
                $query->field('id,name');
            }, 'teacher' => static function ($query) {
                $query->field('id,name');
            }]);

            // 数据列表搜索过滤
            $query->equal('parent_id')->equal('company_id')->dateBetween('create_time');
            $query->like('name')->order('id desc');
        });
    }

    /**
     * 添加打卡任务
     * @auth true
     */
    public function add()
    {
        ModelClassTask::mForm('form');
    }

    /**
     * 编辑打卡任务
     * @auth true
     */
    public function edit()
    {
        ModelClassTask::mForm('form');
    }



    /**
     * 修改
     * @auth true
     */
    public function pass()
    {
        $this->_applyFormToken();
        if ($this->request->isGet()) {
            $this->verify = false;
            ModelClassTask::mForm('pass');
        } else {
            $data = $this->_vali([
                'id.require'                  => '用户ID不能为空！',
             
            ]);
            $ModelClassPlan = ModelClassTask::mk()->findOrEmpty($data['id']);
            if ($ModelClassPlan->isExists() && $ModelClassPlan->save([])) {
             
                $this->success('');
            } else {
                $this->error('');
            }
        }
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        $this->class_list = Classes::mk()->where(['status' => 1])->field('id,name')->select();
        if(request()->isGet()){
            $id = $this->request->get('id');
            if($id){
                $detail = ModelClassTask::mk()->find($id);
                $class = Classes::mk()->find($detail->parent_id);
                if($class){
                    $product_id = $class->product_id;
                    $this->video_list = ModelProductCourse::mk()->where(['product_id' => $product_id])->field('id,name')->select();
                    $this->exam_list = Exampaper::mk()->where(['product_id' => $product_id])->field('id,name')->select();
                }
            }
           
       
        }else{
            if($this->request->post('parent_id')){
                $class = Classes::mk()->find($this->request->post('parent_id'));
                if($class){
                    $data['company_id'] = $class->parent_id;
                }
                if($data['type'] == 2){
                    $data['rel_id'] = $data['video_id'];
                }
                if($data['type'] == 3){
                    $data['rel_id'] = $data['exam_id'];
                }
            }
        }
    }

    /**
     * 修改打卡任务状态
     * @auth true
     */
    public function state()
    {
        $this->_checkInput();
        ModelClassTask::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除学习计划详情
     * @auth true
     */
    public function remove()
    {
        $this->_checkInput();
        ModelClassTask::mDelete();
    }

    /**
     * 检查输入变量
     */
    private function _checkInput()
    {
       
    }


    public function sys_get_list(){
        $class_id = $this->request->post('class_id');
        if($class_id){
            $class = Classes::mk()->find($class_id);
            if(!$class){
                $this->error('未找到班级信息');
            }
            $product_id = $class->product_id;
            $video_list = ModelProductCourse::mk()->where(['product_id' => $product_id])->field('id,name')->select();
            $exam_list = Exampaper::mk()->where(['product_id' => $product_id])->field('id,name')->select();
            $this->success('ok',compact('video_list','exam_list'));
        }else{
            $this->error('参数错误');
        }
    }
    
    
}
